package com.acme.bankapp.service.bank;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.SavingAccount;

public class BankDataLoader {

    public BankDataLoader(Bank bank) {
    	super();
    }

    public void loadBank(Bank bank, String path2F, BankService bankService)  {

    	try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(path2F));
            Bank serBank = (Bank) in.readObject();
            in.close();

            bankService.printBalance(bank);

            System.out.println("-----------------------------------");
            for (Client client : serBank.getClients()) {
             	if (client != null) {
               		System.out.println(client.toString());
               		if (client.getAccount().getClass().getName() == "com.acme.bankapp.domain.bank.CheckingAccount")
               			System.out.println("       Balance+Overdraft: "+client.getAccount().maximumAmountToWithdraw());
               		else System.out.println("       LongBalance: "+((SavingAccount) client.getAccount()).getLongBalance()
               				+"       Balance: "+((SavingAccount) client.getAccount()).getBalance());
               	}
            }
            System.out.println("-----------------------------------");
            
    	} catch (Exception e){
    		e.printStackTrace();
    	}
    }

    public void saveBank(Bank bank, String path2F) throws IOException {
            FileOutputStream fOutStream = new FileOutputStream(path2F);
            ObjectOutputStream objOutStream = new ObjectOutputStream(fOutStream);
            objOutStream.writeObject(bank);
            objOutStream.flush();
            objOutStream.close();
    }
}

